﻿using System;
using System.Data;
using System.Data.SqlClient;
using Eassets.Layered.Domain.Asset;
using System.Collections.ObjectModel;

namespace Eassets.Data.Asset
{
    public class CategoryData
    {
        public void Insert(Category category)
        {
            SqlCommand command = new SqlCommand("SP_InsertCategory"); // Name of the SP
            // command.Parameters.AddWithValue("@CategoryId", category.CategoryID);
            command.Parameters.AddWithValue("@CategoryParentId", category.ParentId);
            command.Parameters.AddWithValue("@Code", category.Code);
            command.Parameters.AddWithValue("@FullCode", category.FullCategoryCode);
            command.Parameters.AddWithValue("@Description", category.Description);
            command.Parameters.AddWithValue("@MoreDetails", category.MoreDetails);
            command.Parameters.AddWithValue("@CategoryFullPath", category.FullPath);
            command.Parameters.AddWithValue("@IsAssetCodeUserDefined", category.IsUserDefined);
            try
            {
                CommonData commonData = new CommonData();
                commonData.ExecuteNonQuery(ConstructCommand(command, category));

            }
            catch (Exception e)
            {

            }

        }

        public Collection<Category> GetAllItems()
        {
            Collection<Category> categories = new Collection<Category>();

            SqlCommand command = new SqlCommand("GetAllCategories");
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);

            while (dataReader.Read())
            {
                Category category = new Category();

                //TODO: Check null.
                category.CategoryID = Convert.ToInt32(dataReader["CategoryID"]);
                category.Code = Convert.ToString(dataReader["Code"]);
                category.Description = Convert.ToString(dataReader["Description"]);
                category.FullCategoryCode = Convert.ToString(dataReader["FullCategoryCode"]);
                category.IsUserDefined = Convert.ToBoolean(dataReader["IsUserDefined"]);
                category.ParentId = Convert.ToInt32(dataReader["ParentId"]);
                category.FullPath = Convert.ToString(dataReader["FullPath"]);

                categories.Add(category);
            }
            return categories;
        }

        public void Update(Category category)
        {
            SqlCommand command = new SqlCommand("SP_UpdateCategory");
            command.Parameters.AddWithValue("@AssetCategoryId", category.CategoryID);
            CommonData commonData = new CommonData();
            commonData.ExecuteNonQuery(ConstructCommand(command, category));

        }

        public void Delete(long categoryId)
        {
            SqlCommand command = new SqlCommand("SP_DeleteAssetCategory");

            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.AddWithValue("@AssetCategoryId", categoryId);

            CommonData commonData = new CommonData();
            commonData.ExecuteNonQuery(command);
        }

        public SqlCommand ConstructCommand(SqlCommand command, Category category)
        {
            command.CommandType = CommandType.StoredProcedure;
            return command;
        }

        public Category GetAssetCategoryById(long AssetCategoryById)
        {
            try
            {
                SqlCommand command = new SqlCommand("sp_GetAssetCategoryById");
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("@AssetCategoryId", AssetCategoryById);

                CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);

                Category AssetCategorys = new Category();
                //  ProductCategoryDomain ProductCategoryd;

                while (dataReader.Read())
                {
                    AssetCategorys = new Category();
                    AssetCategorys.CategoryID = Convert.ToInt32(dataReader["AssetCategoryId"]);
                    if (String.IsNullOrEmpty(Convert.ToString(dataReader["ParentId"])))
                    {
                        AssetCategorys.CategoryID = 0;
                    }
                    else
                    {
                        AssetCategorys.CategoryID = Convert.ToInt32(dataReader["ParentId"]);
                    }
                    AssetCategorys.Description = Convert.ToString(dataReader["Description"]);
                    AssetCategorys.Code = Convert.ToString(dataReader["Code"]);
                    AssetCategorys.FullCategoryCode = Convert.ToString(dataReader["FullCategoryCode"]);
                    AssetCategorys.MoreDetails = Convert.ToString(dataReader["MoreDetails"]);
                    AssetCategorys.IsUserDefined = Convert.ToBoolean(dataReader["IsUserDefined"]);
                    // productCategoryDomain.Add(ProductCategoryd);
                }
                dataReader.Close();
                return AssetCategorys;

            }
            catch (Exception)
            {

                throw;
            }
        }

        public static void Insert(object catego)
        {
            throw new NotImplementedException();
        }

        public void GetAllCategory(Category category)
        {
            throw new NotImplementedException();
        }

        public Collection<Category> GetAllAssetCategory()
        {
            SqlCommand command = new SqlCommand("SP_GetAllCategories");
            command.CommandType = CommandType.StoredProcedure;


            CommonData commonData = new CommonData();
            SqlDataReader dataReader = commonData.ExecuteReader(command);

            Collection<Category> assetCategories = new Collection<Category>();
            Category category;

            while (dataReader.Read())
            {
                category = new Category();
                category.CategoryID = Convert.ToInt16(dataReader["AssetCategoryId"]);
                if (String.IsNullOrEmpty(Convert.ToString(dataReader["ParentId"])))
                {
                    category.ParentId = 0;
                }
                else
                {
                    category.ParentId = Convert.ToInt16(dataReader["ParentId"]);
                }
                category.Description = Convert.ToString(dataReader["Description"]);
                category.Code = Convert.ToString(dataReader["Code"]);
                category.FullCategoryCode = Convert.ToString(dataReader["FullCategoryCode"]);
                category.MoreDetails = Convert.ToString(dataReader["MoreDetails"]);
                category.FullPath = Convert.ToString(dataReader["FullPath"]);
                category.IsUserDefined = Convert.ToBoolean(dataReader["IsUserDefined"]);
                assetCategories.Add(category);
            }
            dataReader.Close();
            return assetCategories;
        }

    }
}
